/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.actions.ui.multiplefileselector;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author lendle
 */
public class FileSelectionButtonCellEditor implements TableCellEditor{
    private JButton button=new JButton("...");
    private JTable lastTable=null;
    private int lastRow=-1;

    public FileSelectionButtonCellEditor() {
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent ae) {
                MultipleFileSelectionModel model=(MultipleFileSelectionModel) lastTable.getModel();
                MultipleFileSelectionConfEntry entry=model.getMultipleFileSelectionConfEntries().get(lastRow);
                int option=-1;
                if(entry.isSave()){
                    option=entry.getFileChooser().showSaveDialog(lastTable);
                }
                else{
                    option=entry.getFileChooser().showOpenDialog(lastTable);
                }
                if(option==JFileChooser.APPROVE_OPTION){
                    entry.setLastSelectedFile(entry.getFileChooser().getSelectedFile());
                    SwingUtilities.invokeLater(new Runnable(){

                        @Override
                        public void run() {
                            lastTable.updateUI();
                        }
                    });
                }
            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable jtable, Object o, boolean bln, int i, int i1) {
        this.lastTable=jtable;
        this.lastRow=i;
        return button;
    }

    @Override
    public Object getCellEditorValue() {
        return null;
    }

    @Override
    public boolean isCellEditable(EventObject eo) {
        return true;
    }

    @Override
    public boolean shouldSelectCell(EventObject eo) {
        return true;
    }

    @Override
    public boolean stopCellEditing() {
        return true;
    }

    @Override
    public void cancelCellEditing() {
        
    }

    @Override
    public void addCellEditorListener(CellEditorListener cl) {
        
    }

    @Override
    public void removeCellEditorListener(CellEditorListener cl) {
        
    }
    
}
